﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MyMoviesBrowserLibrary;

namespace TVManager
{
    /// <summary>
    /// Interaction logic for ChooseImageWindow.xaml
    /// </summary>
    public delegate void BannerSelectedEvent(object sender, string bannerUrl, string bannerType);
    public partial class ChooseImageWindow : Window
    {
        public event BannerSelectedEvent BannerSelected;
        public Series Series { get; set; }
        public string BannerType { get; set; }
        public int? SeasonNumber { get; set; }
        public ChooseImageWindow()
        {
            InitializeComponent();
            cancelButton.Click += new RoutedEventHandler(cancelButton_Click);
            selectButton.Click += new RoutedEventHandler(selectButton_Click);
            imagesListBox.MouseDoubleClick += new MouseButtonEventHandler(imagesListBox_MouseDoubleClick);

            Loaded += new RoutedEventHandler(ChooseImageWindow_Loaded);
        }

        void imagesListBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            ImageSelected();
        }

        void selectButton_Click(object sender, RoutedEventArgs e)
        {
            ImageSelected();
        }

        void ImageSelected()
        {
            Banner banner = imagesListBox.SelectedValue as Banner;
            if (banner != null)
            {
                if(!string.IsNullOrEmpty(banner.ThumbnailPath))
                    banner.ImageUrl = TVManagerBusinessLayer.GraphicsUrl + banner.BannerPath;;
                if (BannerSelected != null)
                    BannerSelected(this, banner.ImageUrl, BannerType);
                Close();
            }
            else
                MessageBox.Show("Please select an image from the list");
        }

        void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        void ChooseImageWindow_Loaded(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(BannerType) || Series == null)
                Close();

            List<Banner> banners = Series.Banners.Where(b => b.BannerType.ToLower() == BannerType.ToLower()
                && (b.LanguageId == Series.LanguageId || !b.LanguageId.HasValue) && (!SeasonNumber.HasValue || (b.Season == SeasonNumber.Value && b.BannerType2 == "season"))).ToList();

            if (banners.Count == 0)
            {
                MessageBox.Show("There are no images to choose from.");
                Close();
                return;
            }

            TVManagerBusinessLayer bl = new TVManagerBusinessLayer();
            
            foreach (Banner banner in banners)
            {
                if (!string.IsNullOrEmpty(banner.ThumbnailPath))
                    banner.ImageUrl = TVManagerBusinessLayer.GraphicsUrl + banner.ThumbnailPath;
                else
                    banner.ImageUrl = TVManagerBusinessLayer.GraphicsUrl + banner.BannerPath;
            }

            imagesListBox.ItemsSource = banners;
        }
    }
}
